let listPoints = ref (""::[])
let listFaces = ref (""::[])
let listTexture = ref (""::[])

let coefficient = 80

let pas = 10
let minH = 0.
let maxH = 2000.

let main () =
    Sdl.init [`EVERYTHING];
    
    let img = Sdlloader.load_image Sys.argv.(1) in
    let (w,h) = ((Sdlvideo.surface_info img).Sdlvideo.w,
	(Sdlvideo.surface_info img).Sdlvideo.h)
    in
    begin
    
    let i = ref 0
    and j = ref 0
    in
    (* ajout des points *)
    while !i <= (h - 1 - pas) do
    j := 0;
	while !j <= (w- 1 - pas) do
	begin
	    let (r1,g1,b1) = Sdlvideo.get_pixel_color img !j !i in
	    let height1 = int_of_float(((float)(255-r1) /. 255.) *. maxH) in
	    let point1 = "v " ^ string_of_int(!j * coefficient) ^ " " ^
		string_of_int(!i * coefficient) ^ " " ^ string_of_int(height1)
	    in listPoints := point1::!listPoints;
	    print_endline point1;
	    j := !j + pas
	end
	done;
	i := !i + pas
    done;


    let w1 = (w/pas)
    and h1 = (h/pas)
    in
    print_endline ("ici" ^ string_of_int (w1));
    (* ajout des faces *)
    for i = 0 to (w1 - 2) do
	for j = 0 to (h1 - 2) do
	    let face1 = "f " ^ string_of_int(j * w1 + i + 1) ^ " " ^
	    string_of_int(j * w1 + i + 2) ^ " " ^
	    string_of_int((j + 1) * w1 + i + 2)
	    and face2 = "f " ^ string_of_int(j * w1 + i + 1) ^ " " ^
	    string_of_int((j + 1) * w1 + i + 2) ^ " " ^
	    string_of_int((j + 1) * w1 + i + 1)
	    in
		print_endline (face1);
		print_endline (face2)
	done;
    done
    end

let _ =
    main ()
